﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Ria.ApplicationServices;
using System.Windows.Ria.Data;
using System.Windows.Shapes;
using CARE.Web.Services;
using GraphInformation;
using System.Xml.Linq;

namespace CARE
{
    public partial class InoculationPanel : UserControl
    {
        private IWizardButtonController wizardButtonController;

        private GraphProvider gp;

        private Graph output;
        private Graph graph;

        List<KeyValuePair<GraphElement, double>> map;

        int vaccinesCount;

        public InoculationPanel(IWizardButtonController sw)
        {
            InitializeComponent();

            gp = new GraphProvider();

            wizardButtonController = sw;

            InvokeOperation<String[]> invokeOpGetAvailableMethodsOfCentrality = gp.GetAvailableMethodsOfCentrality();
            invokeOpGetAvailableMethodsOfCentrality.Completed += new System.EventHandler(invokeOpinvokeOpGetAvailableMethodsOfCentrality_Completed);
        }

        public void Init()
        {
            this.graph = InoculationParameters.getInstance().SocialNetworkGraph;
            this.map = null;
            this.PatientDetails.Text = string.Empty;
            PatientsList.Items.Clear();
        }

        private void invokeOpinvokeOpGetAvailableMethodsOfCentrality_Completed(object sender, EventArgs e)
        {
            InvokeOperation<String[]> invokeOp = (InvokeOperation<String[]>)sender;

            CentralitiesMeasures.Items.Clear();
            foreach (string item in invokeOp.Value)
                CentralitiesMeasures.Items.Add(item);
            if (CentralitiesMeasures.Items.Count > 0)
            {
                CentralitiesMeasures.SelectedIndex = 0;
                CentralityMeasureDescription.Text = GetCentralityMeasureDescription(CentralitiesMeasures.SelectedItem.ToString());
            }
        }

        string GetCentralityMeasureDescription(string name)
        {
            Type res = typeof(CARE.Resources.Resources);
            string description = (string)res.GetProperty(name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null, null);
            return description;
        }

        private void CentralitiesMeasures_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	if (CentralitiesMeasures.SelectedItem != null)
				CentralityMeasureDescription.Text = GetCentralityMeasureDescription(CentralitiesMeasures.SelectedItem.ToString());
		}

        private void GeneratePatientsList_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            int count;
            if (Int32.TryParse(VaccinesCount.Text, out count) == false)
                VaccinesCount.Text = "Insert integer";
            else if (count <= 0)
                VaccinesCount.Text = "Positive only";
            else if (CentralitiesMeasures.SelectedItem != null)
            {
                //GeneratePatientsList.IsEnabled = false;
                //vaccinesCount = count;
                //if (graphType == typeof(string))
                //    cl.ComputeCentralityFromGraphMLAsync(CentralitiesMeasures.SelectedItem.ToString(),
                //        (string)graph);
                //else if (graphType == typeof(GraphProvider.Graph))
                //    cl.ComputeCentralityAsync(CentralitiesMeasures.SelectedItem.ToString(),
                //        (GraphProvider.Graph)graph);       
            }
        }

        private void PatientsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            this.PatientDetails.Text = string.Empty;

            if (PatientsList.SelectedItem != null)
            {
                string patient = PatientsList.SelectedItem.ToString();
                int i = 1;
                foreach (var item in map)
                {
                    string name = i.ToString() + ". ";
                    string surname = string.Empty;
                    foreach (PairKeyValue key in item.Key.Keys)
                        if (key.Key == "name")
                            name += key.Value;
                        else if (key.Key == "surname")
                            surname += key.Value;
                    if (patient == name + " " + surname)
                    {
                        foreach (PairKeyValue key in item.Key.Keys)
                            this.PatientDetails.Text += key.Key + " = " + key.Value + "\n";
                        break;
                    }
                    i++;
                }
            }
        }

        void ComputeCentralityCompleted()
        {
            PatientsList.Items.Clear();

            map = new List<KeyValuePair<GraphElement, double>>();
            foreach (var item in ((Graph)output).Vertices)
                foreach (PairKeyValue key in ((GraphElement)item).Keys)
                    if (key.Key == "importance")
                        map.Add(new KeyValuePair<GraphElement, double>((GraphElement)item, Convert.ToDouble(key.Value)));
            map.Sort((firstPair, nextPair) =>
            {
                return -firstPair.Value.CompareTo(nextPair.Value);
            });

            int i = 1;
            foreach (var item in map)
            {
                string name = i.ToString() + ". ";
                string surname = string.Empty;
                foreach (PairKeyValue key in item.Key.Keys)
                    if (key.Key == "name")
                        name += key.Value;
                    else if (key.Key == "surname")
                        surname += key.Value;
                PatientsList.Items.Add(name + " " + surname);
                i++;
                if (i > vaccinesCount)
                    break;
            }
            GeneratePatientsList.IsEnabled = true;
        }

    }
}
